<template>
  <div class="user-edit">
    <!-- Drawer抽屉 -->
    <Modal
      :title="title"
      v-model="visible"
      width="600"
      :loading="visible"
      :mask-closable="type == '0'"
    >
      <div :style="{ maxHeight: maxHeight }" class="drawer-content">
        <Form
          ref="form"
          :model="form"
          :rules="ruleValidate"
          label-position="top"
        >
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="控制器序列号：" prop="serialNo">
                <Input v-model="form.serialNo" placeholder="请填写控制器序列号" />
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="打印机名称：" prop="title">
                <Input v-model="form.title" placeholder="请填写打印机名称" />
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="打印机位置：" prop="address">
                <Input v-model="form.address" placeholder="请填写打印机位置" />
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="纸张单价：（元/每页）" prop="price">
                <Input v-model="form.price" placeholder="请填写纸张单价" />
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="24">
              <FormItem label="积分消费规则：" prop="selectIntegra">
                <Select v-model="form.selectIntegra" multiple placeholder="请选择">
                  <Option v-for="item in integralList" :value="item.id" :key="item.id">{{ item.title }}</Option>
              </Select>
              </FormItem>
            </Col>
            
          </Row>
        </Form>
      </div>
      <div slot="footer">
        <Button type="primary" size="large" :loading="submitLoading" @click="submit">保存</Button>
        <Button size="large" @click="cancel">取消</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { getFindAll, scIntegralExpenseGetAll, scPrintAdd } from "@/api/index";
export default {
  name: "printManageEdit",
  components: {
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
    },
    type: {
      type: String,
      default: "0",
    },
  },
  data() {
    return {
      visible: this.value,
      title: "添加打印机",
      passColor: "",
      submitLoading: false,
      maxHeight: 510,
      companyNatureList: [], //企业性质列表
      integralList: [], //积分规则列表
      selectIntegra: [], //选中的积分规则
      form: {
      },
      searchForm: {
        pageNumber: 1,
        pageSize: 100,
        sort: "createTime",
        order: "desc",
      },
      ruleValidate: {
        title: [
          { required: true, message: "请填写打印机名称", trigger: "blur" }
        ],
        serialNo: [
          { required: true, message: "请填写控制器序列号", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请填写打印机位置", trigger: "blur" }
        ],
        price: [
          { required: true, message: "请填写张单价", trigger: "blur" }
        ],
        selectIntegra: [
          { required: true, message: "请选择积分消费规则", trigger: "change", type: "array" }
        ],
      },
      
    };
  },
  methods: {
    cancel() {
      this.visible = false
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if(valid) {
          let apiType = ''
          if(this.type == '1') {
            //修改
            apiType = 'update'
          }else if (this.type == '2') {
            //添加
            apiType = 'add'
          }
          let idList = {}
          this.form.selectIntegra.forEach((item, index) => {
            idList['printIntegralExpenses['+index+'].integralExpenseId'] = item
          })
          this.form = {
            ...this.form,
            ...idList
          }
          scPrintAdd(apiType,this.form).then(res => {
            if (res.success) {
              this.$Message.success("操作成功")
              this.visible = false
              this.$emit("on-submit", true);
            }
          })
        }
      });
    },
    setCurrentValue(value) {
      if (value === this.visible) {
        return;
      }
      this.$refs.form.resetFields()
      if (this.type == "1") {
        this.title = "信息修改";
        this.maxHeight =
          Number(document.documentElement.clientHeight - 121) + "px";
      } else if (this.type == "2") {
        this.title = "添加打印机";
        this.maxHeight =
          Number(document.documentElement.clientHeight - 121) + "px";
      }
      // 清空数据
      if (this.type == "1") {
        // 回显数据
        let selectIntegra = []
        this.data.printIntegralExpenses.forEach(item => {
          selectIntegra.push(item.integralExpenseId)
        })
        this.form = {
          ...this.data,
          selectIntegra
        }
      } else {
        // 添加
        this.form = {
        };
      }
      this.visible = value;
    },
    
    scIntegralExpenseGetAllData() {
      scIntegralExpenseGetAll().then(res => {
        if (res.success) {
          this.integralList = res.result
        }
      })
    }
  },
  watch: {
    value(val) {
      this.setCurrentValue(val);
    },
    visible(value) {
      this.$emit("input", value);
    },
  },
  mounted() {
    // this.getFindAllData()
    this.scIntegralExpenseGetAllData()
  }
};
</script>

<style lang="less">
@import "@/styles/drawer-common.less";
.drawer-content{
  overflow: visible;
}
</style>

